﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AgileLogistics.View.Nocite
{
    /// <summary>
    /// Add.xaml 的交互逻辑
    /// </summary>
    public partial class Add : Page
    {
        ViewModel.NociteViewModel vm;
        Model.DBContext dbContext = new Model.DBContext();
        public Add()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.NociteViewModel;

            vm.CurrNocite = new  Model.Notice ();

            vm.UserList = dbContext.DB.Queryable<Model.Users>().ToList();
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            //this.Close();
        }

        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty( vm.CurrNocite.Title))
            {
                Growl.WarningGlobal("请输入通知标题");
                return;
            }

            if (string.IsNullOrEmpty(vm.CurrNocite.NoticeContent))
            {
                Growl.WarningGlobal("请输入通知内容");
                return;
            }

            Save();
        }

        private void btn_SaveAndSend_Click(object sender, RoutedEventArgs e)
        {
            Save();


        }


        private void Save()
        {

            vm.CurrNocite.CreateTime = DateTime.Now;



            vm.CurrNocite = dbContext.DB.Insertable<Model.Notice>(vm.CurrNocite).ExecuteReturnEntity();

            //添加通知接受对象
            foreach (var user in vm.UserList)
            {
                var noticeObj = new Model.NoticeUsers()
                {
                    NoticeId = vm.CurrNocite.Id,
                    UserId = user.Id,
                    IsRead = false,

                };
                dbContext.DB.Insertable<Model.NoticeUsers>(noticeObj).ExecuteCommand();
            }


            Growl.SuccessGlobal("发布成功！");


            vm.NociteList = dbContext.DB.Queryable<Model.Notice>().ToList();
            
        }
    }
}
